/**
 * @(#)NumUtil.java 2016年11月10日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.util;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
public class NumUtil {
	public static final Logger LOGGER = LoggerFactory.getLogger(NumUtil.class);

	/**
	 * 
	 * randomChoiceIndex:根据值的大小，随机选择一个元素
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param list
	 * @return
	 */
	public static int randomChoiceIndex(Integer[] list) {
		int index = -1;
		if (list != null && list.length > 0) {
			if (list.length == 1) {
				index = 0;
			} else {
				int total = 0;
				for (Integer i : list) {
					total += i;
				}
				NumberFormat numFormat = NumberFormat.getInstance();
				numFormat.setMaximumFractionDigits(1);
				double rate = Double.parseDouble(numFormat.format(Math.random()));
				double target = rate * total;
				int length = list.length;
				int sum = 0;
				for (int i = 0; i < length; i++) {
					sum += list[i];
					if (sum >= target) {
						index = i;
						break;
					}
				}
			}
		}
		return index;
	}

	public static int randomChoiceIndex2(List<Double> list) {
		int index = -1;
		if (list != null && list.size() > 0) {
			if (list.size() == 1) {
				index = 0;
			} else {
				Double total = 0d;
				for (Double i : list) {
					total += i;
				}
				NumberFormat numFormat = NumberFormat.getInstance();
				numFormat.setMaximumFractionDigits(1);
				double rate = Double.parseDouble(numFormat.format(Math.random()));
				double target = rate * total;
				int length = list.size();
				int sum = 0;
				for (int i = 0; i < length; i++) {
					sum += list.get(i);
					if (sum >= target) {
						index = i;
						break;
					}
				}
				if (index == -1) {
					index = 0;
				}
			}
		}
		return index;
	}

	public static int randomChoiceIndex(Double[] list) {
		int index = -1;
		if (list != null && list.length > 0) {
			if (list.length == 1) {
				index = 0;
			} else {
				Double total = 0d;
				for (Double i : list) {
					total += i;
				}
				NumberFormat numFormat = NumberFormat.getInstance();
				numFormat.setMaximumFractionDigits(7);
				double rate = Double.parseDouble(numFormat.format(Math.random()));
				double target = rate * total;
				int length = list.length;
				double sum = 0;
				for (int i = 0; i < length; i++) {
					sum += list[i];
					if (sum >= target) {
						index = i;
						break;
					}
				}
				if (index == -1) {
					index = 0;
				}
			}
		}
		return index;
	}
	
	/**
	 * 
	 * randomNums:数字随机
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param nums
	 * @return
	 */
	public static Integer[] randomNums(Integer[] nums) {
		if (nums != null && nums.length > 0) {
			Random random = new Random();
			int size = nums.length;
			for (int i = 0; i < size; i++) {
				int p = random.nextInt(size);
				int tmp = nums[i];
				nums[i] = nums[p];
				nums[p] = tmp;
			}
			random = null;
		}
		return nums;
	}

	/**
	 * 
	 * randomChoiceIndex:根据值的大小，随机选择一个元素
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @param list
	 * @return
	 */
	public static int randomChoiceIndex(List<Integer> list) {
		if(list == null || list.isEmpty()){
			return -1;
		}
		return randomChoiceIndex(list.toArray(new Integer[0]));
	}

	/**
	 * 精确的浮点数运算-加法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static float add(float... values) {
		BigDecimal sum = new BigDecimal(0 + "");
		for (int i = 0; i < values.length; i++) {
			BigDecimal b = new BigDecimal(values[i] + "");
			sum = sum.add(b);
		}
		return sum.setScale(6, BigDecimal.ROUND_HALF_UP).floatValue();
	}

	/**
	 * 精确的浮点数运算-乘法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static float multiply(float...values) {
		BigDecimal product = new BigDecimal(1 + "");
		for (int i = 0; i < values.length; i++) {
			BigDecimal b = new BigDecimal(values[i] + "");
			product = product.multiply(b);
		}
		return product.setScale(6, BigDecimal.ROUND_HALF_UP).floatValue();
    }
	
	/**
	 * 精确的浮点数运算-除法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static float divide(float a, float b) {
		BigDecimal a1  = new BigDecimal(a + "");
		BigDecimal b1  = new BigDecimal(b + "");
		BigDecimal quotient = a1.divide (b1, 6 , BigDecimal.ROUND_HALF_UP);
		return quotient.floatValue();
    }
	
	/**
	 * 精确的浮点数运算-减法
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static float subtract(float a, float b) {
		BigDecimal a1  = new BigDecimal(a + "");
		BigDecimal b1  = new BigDecimal(b + "");
		BigDecimal difference = a1.subtract(b1);
		return difference.setScale(6, BigDecimal.ROUND_HALF_UP).floatValue();
    }
}
